from rest_framework import serializers
from djoser.serializers import UserCreateSerializer
from rest_framework.validators import UniqueValidator
from django.contrib.auth import get_user_model
from rest_framework import serializers
from movie.serializers import MovieSerializer
from movie.models import Movie


User = get_user_model()

class CustomUserCreateSerializer(UserCreateSerializer):
    email = serializers.EmailField(
        validators=[UniqueValidator(queryset=User.objects.all(), message='邮箱已经存在')],
        error_messages={
            'invalid': '请输入有效的邮箱地址'
        }
    )
    username = serializers.CharField(
        min_length=6,
        max_length=50,
        error_messages={
            'min_length': '用户名长度不能少于6个字符',
            'max_length': '用户名长度不能超过50个字符'
        }
    )
    password = serializers.CharField(
        min_length=6,
        max_length=50,
        write_only=True,
        error_messages={
            'min_length': '密码长度不能少于6个字符',
            'max_length': '密码长度不能超过50个字符'
        }
    )

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')


class UserCollectionSerializer(serializers.ModelSerializer):
    """用户收藏电影的序列化器"""
    class Meta:
        model = Movie
        fields = [
            'id', 'movie_name', 'release_year', 'director', 'scriptwriter', 'actors',
            'image_url', 'rate','types','region','quality', 'created_at'
        ]



